/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: chenju
 * @LastEditTime: 2023-01-17 16:33:58
 */
import { reactive } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import type { CTableProps } from '@/components/CTable'
import { getUsersList } from '@/apis/system/user'
import type { GetUsersListParams } from '@/apis/system/model/userModel'
import type { BasicPagingListParams } from '@/apis/basicModel'

export const useFactorList = () => {
  // 搜索字段及其他
  const searchForm = reactive<GetUsersListParams>({
    realName: ''
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = [
    {
      name: '用户名称',
      field: 'realName',
      type: 'input'
    }
  ]

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: GetUsersListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getUsersList(params)
  }

  // 处理参数
  const handleParams = (): GetUsersListParams => {
    // 参数
    let params: GetUsersListParams = {
      realName: searchForm.realName
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '用户名称',
      field: 'realName',
      slot: 'realNameSlot',
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '登录邮箱',
      field: 'username',
      width: '180',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '手机号',
      field: 'phone',
      width: '120',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '工号',
      field: 'employeeNo',
      width: '120',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '所属组织',
      field: 'orgName',
      minWidth: '210',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '用户角色',
      field: 'role',
      slot: 'roleSlot',
      minWidth: '210',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人/创建时间',
      field: 'createBy',
      slot: 'creatorSlot',
      width: '160',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'op',
      title: '操作',
      field: 'op',
      slot: 'op',
      width: '180',
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
